//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Windows.Forms;
using EnvDTE;

namespace DBProBridge
{
    /// <summary>
    /// UI to edit <see cref="Options"/>.
    /// </summary>
    public partial class OptionsPage : UserControl, IDTToolsOptionsPage
    {
        #region Fields
        static Options _propertiesObject = new Options(); 
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="OptionsPage"/> class.
        /// </summary>
        public OptionsPage()
        {
            InitializeComponent();
        } 
        #endregion

        #region IDTToolsOptionsPage Members

        /// <summary>
        /// Gets the properties.
        /// </summary>
        /// <param name="PropertiesObject">The properties object.</param>
        public void GetProperties(ref object PropertiesObject)
        {
            //Return an object which is accessed by the method call DTE.Properties(category, subcategory)
            PropertiesObject = _propertiesObject;
        }

        /// <summary>
        /// Called when [after created].
        /// </summary>
        /// <param name="DTEObject">The DTE object.</param>
        public void OnAfterCreated(DTE DTEObject)
        {
            _propertiesObject = SerializationUtility.DeserializeFromFile<Options>(
                OptionsPath);
            if (_propertiesObject == null)
                _propertiesObject = new Options();

            propertyGrid.SelectedObject = _propertiesObject;
        }

        public void OnCancel()
        {
        }

        public void OnHelp()
        {
            MessageBox.Show("TODO: Display Help");
        }

        public void OnOK()
        {
            // Save options to the file.
            SerializationUtility.Serialize2File(
                _propertiesObject,
                OptionsPath);
        }

        #endregion

        #region Public Properties
        /// <summary>
        /// Gets the options path.
        /// </summary>
        /// <value>The options path.</value>
        public static string OptionsPath
        {
            get
            {
                return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
                                + @"\" + DBProBridge.ProductName + @"\" +
                                DBProBridge.ProductName + "Options.xml";
            }
        }
        #endregion
    }
}